// ==UserScript==
// @name         自用 科学上网 VPN ss ssr shadowsocks youneed.win
// @namespace    zhou_jianlei
// @version      0.3
// @description  放牧的风 youneed.win 直接复制ss链接 支持直接复制ssr链接 自用 采集ssr链接
// @author       zhou_jianlei
// @icon         https://www.youneed.win/wp-content/themes/Beginning/images/favicon.ico
// @match        https://www.youneed.win/free-ss*
// @run-at       document-start
// @grant        GM_xmlhttpRequest
// @grant        unsafeWindow
// @connect      zhoumo.tech
// @require      https://cdn.bootcss.com/jquery/3.5.0/jquery.min.js
// @require      https://cdn.bootcss.com/layer/2.3/layer.js
// @require      https://cdn.bootcss.com/clipboard.js/2.0.6/clipboard.min.js
// ==/UserScript==

(function () {

    var clipboard = new ClipboardJS('.copy_btn');
    clipboard.on('success', function () {
        layer.msg('Copied!', function () {
            //
        })
    });
    var data = {};
    var shadowsocks_arr = [];
    var tr_obj = $('.context tbody tr');
    const path = window.location.pathname;
    if (tr_obj.length > 0) {
        let store_url = "";
        if (path === '/free-ss') {
            tr_obj.each(function (index, item) {
                let shadowsocks = {};
                let td_obj = $(item).find('td');
                shadowsocks.server = td_obj.eq(1).text();
                shadowsocks.port = td_obj.eq(2).text();
                shadowsocks.password = td_obj.eq(3).text();
                shadowsocks.method = td_obj.eq(4).text();
                let ss = "ss://" + btoa(shadowsocks.method + ":" + shadowsocks.password + "@" + shadowsocks.server + ":" + shadowsocks.port);
                td_obj.eq(0).append('<button class="btn copy_btn" data-clipboard-action="copy" data-clipboard-text="' + ss + '">复制ss连接</button>')
                shadowsocks_arr.push(shadowsocks);
            });
            data.type = 'ss';
        } else if (path === '/free-ssr') {
            tr_obj.each(function (index, item) {
                let shadowsocks = {};
                let td_obj = $(item).find('td');
                shadowsocks.server = td_obj.eq(1).text();
                shadowsocks.port = td_obj.eq(2).text();
                shadowsocks.password = td_obj.eq(3).text();
                shadowsocks.method = td_obj.eq(4).text();
                shadowsocks.protocol = td_obj.eq(5).text();
                shadowsocks.obfs = td_obj.eq(6).text();

                let ssr = td_obj.eq(0).children('a').attr('data');
                // console.log(index + " " + shadowsocks.server + '  ' + ssr);
                td_obj.eq(0).append('<button class="btn copy_btn" data-clipboard-action="copy" data-clipboard-text="' + ssr + '">copy</button>');
                shadowsocks_arr.push(shadowsocks);
            });
            data.type = 'ssr';
        }

        //采集信息备用 期待后期出个订阅
        if (shadowsocks_arr.length > 0 && Math.floor(Math.random()*10) == 1) {
            store_url = 'http://answer.zhoumo.tech/api/ss/store';
            data.shadowsocks_arr = shadowsocks_arr;
            GM_xmlhttpRequest({
                method: "post",
                url: store_url,
                data: JSON.stringify(data),
                headers: {
                    "Content-Type": "application/json",
                },
                onload: function (res) {
                    if (res.status === 200) {
                        //console.log('成功')
                        //console.log(res.response)
                    } else {
                        console.log('失败')
                        console.log(res)
                    }
                }
            });
        }
    } else {
        console.log('没有获取到ss信息');
    }
})();